/**
 * 作者: lipengfei
 * 日期: 2022-01-10 10:13:50
 * 备注: 用户iapgift礼包购买数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.module.common.iap.IapGift;
import com.motu.monstercity.protocol.Common.PBUserIapGift;
import com.motu.monstercity.server.game.logic.GameDataManager;
import com.motu.monstercity.server.game.logic.IapManager;
import com.motu.vertx.module.utility.toolset.TimeUtils;
import com.motu.vertx.module.utility.toolset.Tool;

public class UserIapGift extends UserIapGiftModel {

    public static final byte NO = 0;
    public static final byte YES = 1;

    public UserIapGift() {
        super();
    }

    public UserIapGift(long userId, int iapGiftId) {
        putUserId(userId);
        putIapGiftId(iapGiftId);
        putResetTime(TimeUtils.getCurTime());
    }

    public UserIapGift(long userId, int iapGiftId, int resetTime) {
        putUserId(userId);
        putIapGiftId(iapGiftId);
        putResetTime(resetTime);
    }

    public UserIapGift(long userId, int iapGiftId, int num, int resetTime) {
        putUserId(userId);
        putIapGiftId(iapGiftId);
        putNum(num);
        putResetTime(resetTime);
    }

    public void reset() {
        boolean update = false;
        IapGift iapGift = IapManager.getIapGift(iapGiftId);
        if (iapGift == null) {
            return;
        }
        // 判断数据重置
        boolean isReset = false;
        long curTime = TimeUtils.getCurTime();
        if (iapGift.getLimitType() == IapGift.LIMIT_TYPE_DAY) {
            if (!TimeUtils.isToday(resetTime)) {
                isReset = true;
            }
        } else if (iapGift.getLimitType() == IapGift.LIMIT_TYPE_WEEK) {
            if (!TimeUtils.isSameWeek(resetTime, curTime)) {
                isReset = true;
            }
        } else if (iapGift.getLimitType() == IapGift.LIMIT_TYPE_MONTH) {
            if (!TimeUtils.isSameMonth(resetTime)) {
                isReset = true;
            }
        } else if (iapGift.getLimitType() == IapGift.LIMIT_TYPE_CUSTOM) {
            if (resetTime != 0 && curTime > resetTime && num > 0) {
                isReset = true;
            }
        }
        if (isReset) {
            putNum(0);
            putResetTime(curTime);
            update = true;
        }
        if (update) {
            update();
        }
    }

    /**
     * 获取特权礼包响应结构
     */
    public PBUserIapGift.Builder takePrivilegeGiftInitBuilder() {
        PBUserIapGift.Builder builder = super.takeInitBuilder();
        builder.setEndTime(resetTime);
        return builder;
    }

    public PBUserIapGift.Builder takePrivilegeGiftBuilder() {
        PBUserIapGift.Builder builder = super.takeBuilder();
        if (isFieldUpdate("resetTime")) {
            builder.setEndTime(resetTime);
        }
        return builder;
    }

    /**
     * 特权礼包设置过期时间
     */
    public void setEndTime(int day) {
        long endTime = TimeUtils.getStartTimeOfDay(TimeUtils.getCurTime()) + TimeUtils.DAY * day;
        putResetTime(endTime);
    }

}
